<?php
/**
 *	@author wyl
 *	@date 2015-11-2 11:56:50
 */
Yii::import('application.components.dao.class.AuctionClear');
Yii::import('dao.auction.AuctionCacheData');
class AuctionStatus extends CFormModel{
	const AUCTION_STATUS_END=2;
	const AUCTION_STATUS_LIVE=1;
	const AUCTION_STATUS_UNCOMING=0;
	public $auctionId;
	public $status;
	
	public function rules(){
	    return array(
	       array('auctionId','hasAutoBid','on' => 'endAuction')  
	    );
	}
	
	public function hasAutoBid($attribute, $params){
	   $hasAutoBidSQL = '
	       SELECT
	           COUNT(DISTINCT customer_id) AS autoBidCount
	       FROM
	           autobid
	       WHERE
	           autobid.auction_id = '.$this->auctionId.'
	       AND
	           autobid.amount > 0
	           
	   ';
	   $hasAutoBid=Yii::app()->db->createCommand($hasAutoBidSQL)->queryRow();
	   if($hasAutoBid['autoBidCount'] > 1){
	       $this->addError('auto_bid','auto bid no end');
	   }
	}
	
	public function start(){	    
		$statusSql='
					UPDATE  
	                	auction_pool
	            	SET
	                	auction_pool.status ='.self::AUCTION_STATUS_LIVE.'
	            	WHERE
	                	auction_pool.auction_id = ' . $this->auctionId . '
       			 ';
		$modifyResult=Yii::app()->db->createCommand($statusSql)->execute();
		
		$auctionInfoSql='
					UPDATE
	                	auction_info
	            	SET
	                	auction_info.status ='.self::AUCTION_STATUS_LIVE.'
	            	WHERE
	                	auction_info.id = ' . $this->auctionId . '
       			 ';
		Yii::app()->db->createCommand($auctionInfoSql)->execute();
		
		if ($modifyResult){ 
			$auctionCache = new AuctionCacheData();
			$auctionCache->findGNCByPk($this->auctionId);
		}
		return $modifyResult;
	}
	
	public function end(){
	    if (!$this->validate()){
	        return false;
	    }
		if(Yii::app()->lock->check('AUTOBID_GEARMAN_LOCK:' . $this->auctionId) || Yii::app()->lock->check('BID_AUCTION_LOCK:' . $this->auctionId)){
			return false;
		}
		Yii::app()->lock->get('AUCTION_END_LOCK:' . $this->auctionId);
		$statusSql='
					UPDATE  
	                	auction_pool
	            	SET
	                	auction_pool.status ='.self::AUCTION_STATUS_END.'
	            	WHERE
	                	auction_pool.auction_id = ' . $this->auctionId . '
       			 ';
		
		$endResult=Yii::app()->db->createCommand($statusSql)->execute();
		$auctionCacehData=new AuctionCacheData();
		$auctionCacehData->update($this->auctionId, array('status'=>self::AUCTION_STATUS_END));
		
		$syncResult=false;
		$auctionPool=new AuctionPool();
		$auctionPool=$auctionPool->findByAttributes(array('auction_id'=>$this->auctionId));
		if (!empty($auctionPool)) {
		    $updateSql=	'
					UPDATE
	                	auction_info
	            	SET
						auction_info.end_time='.$auctionPool->end_time.',
						auction_info.winner_price='.$auctionPool->winner_price.',
	                	auction_info.status =2,
						auction_info.winner_id='.$auctionPool->winner_id.',
						auction_info.winner_time='.$auctionPool->winner_time.'
	            	WHERE
	                	auction_info.id = ' . $this->auctionId . '
       	    ';
		    $syncResult=Yii::app()->db->createCommand($updateSql)->execute();
		
		    $auctionPool=new AuctionPool();
		    $auctionPool->deleteAllByAttributes(array('auction_id'=>$this->auctionId));
		
		    $auctionCacehData=new AuctionCacheData();
		    $auctionCacehData->update($this->auctionId, array('status'=>self::AUCTION_STATUS_END));
		}
		
		if ($syncResult) {
		    $auctionClear=new AuctionClear();
		    $auctionClear->auctionId=$this->auctionId;
		    $auctionClear->clear();
		}
		return $syncResult;
	}
	
	public function getStatusList($status){
		$auctionPool=new AuctionPool();
		$statusList=array();
		$criteria=new CDbCriteria();
		switch ($status){
			case self::AUCTION_STATUS_UNCOMING:
				$criteria->addCondition('start_time<='.time());
				break;
			case self::AUCTION_STATUS_LIVE:
				$criteria->addCondition('end_time<='.(time()-2));
				break;
		}
		$criteria->compare('status', $status);
		$auctionPool=$auctionPool->findAll($criteria);
		if(!empty($auctionPool)){
			foreach ($auctionPool as $auction){
				$className=get_class($this);
				$obj=new $className($this->getScenario());
				$obj->auctionId=$auction->auction_id;
				$obj->status=$status;
				$statusList[]=$obj;
			}
		}
		return $statusList;
	}
}